package leeon.mobile.BBSBrowser;

import java.util.ArrayList;
import java.util.List;

import leeon.kaixin.wap.action.PictureAction;
import leeon.kaixin.wap.models.Picture;
import leeon.kaixin.wap.models.StatusPic;
import leeon.kaixin.wap.util.HttpUtil;
import android.app.ListActivity;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class KaixinPictureActivity extends ListActivity {
	private List<Picture> list;
	private StatusPic status;
	private String verify;
	
	private Handler handler = new Handler();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		super.onCreate(savedInstanceState);
		
		status = (StatusPic)this.getIntent().getSerializableExtra("status");
		verify = KaixinMainActivity.verify;
		
		list = new ArrayList<Picture>();
		getListView().setBackgroundColor(Color.WHITE); 
		getListView().setDivider(getResources().getDrawable(R.drawable.kaixin_divider));
		setListAdapter(new PicListAdapter(list));
		scrollAction.addActionWhenScroll(getListView());
				
		if (verify != null) {
			scrollAction.refresh();
		}
		
		if (customTitleSupported) {
			getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.kaixin_titlebar);
			TextView title = (TextView)findViewById(R.id.kaixinTitle);
			title.setText(status.getUname() + "上传的照片");
		}
	}
	
	@Override 
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
		} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
		}
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, final int position, long id) {
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		boolean result = super.onCreateOptionsMenu(menu);
		menu.add(0, 1, 1, "刷新动态").setIcon(android.R.drawable.ic_menu_rotate);
        return result;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 1:
			scrollAction.refresh();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	private void listPic(final int totalItemCount) {
		UIUtil.runActionInThread(this, new UIUtil.ActionInThread<Picture>() {
			@Override
			public void action() throws NetworkException, ContentException {
				if (verify != null && status != null) {
					if (list.size() == 0)
						bglist = PictureAction.listPicture(verify, status, "2");
					else
						bglist = PictureAction.listPicture(list.get(totalItemCount-1));
				}
			}
			@Override
			public void actionFinish() {
				if (bglist != null) {
					list.addAll(bglist);
					((BaseAdapter)KaixinPictureActivity.this.getListView().getAdapter()).notifyDataSetChanged();
				}
			}
			
			@Override
			public void actionError() {
				scrollAction.rollback();
			}
		});
	}
	
	private KaixinListScrollAction scrollAction = new KaixinListScrollAction();
	private class KaixinListScrollAction extends UIUtil.ActionInScroll {
		
		public KaixinListScrollAction() {
			new UIUtil().super();
		}
		
		@Override
		protected void actionClear() {
			list.clear();
		}

		@Override
		protected void actionScroll(int totalItemCount) {
			listPic(totalItemCount);
		}

		@Override
		protected void actionRemove(Object o) {
			list.remove(o);
		}
		
		@Override
		protected int pageSize() {
			return 5;
		}
		
	}
	
	private class PicListAdapter extends BaseAdapter {

		private List<Picture> list;

		public PicListAdapter(List<Picture> list) {
			this.list = list;
		}

		public int getCount() {
			return list.size();
		}

		public Object getItem(int position) {
			return list.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View tv;
			if (convertView == null) {
				tv = LayoutInflater.from(KaixinPictureActivity.this).inflate(R.layout.kaixin_pic_item, parent, false);
			} else {
				tv = convertView;
			}
			
			ImageView img = (ImageView)tv.findViewById(R.id.picImage);
			TextView text = (TextView)tv.findViewById(R.id.picInfo);
			
			Picture p = list.get(position);
			text.setText("("+p.getNo()+"/"+p.getCount()+")"+p.getTitle());
			UIUtil.loadImageInThread(p.getUrl(), KaixinPictureActivity.this, handler, img, HttpUtil.newInstance());
			
			UIUtil.viewBigImage(p.getOurl(), img, KaixinPictureActivity.this, HttpUtil.newInstance());
			
//			if (p.getUrl().indexOf("_wapb") != -1) {
//				UIUtil.viewBigImage(p.getUrl().replace("_wapb", ""), img, KaixinPictureActivity.this);
//			}
			return tv;
		}
	}
}
